package com.tianruan.psychological.common.utils.encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * <Description> <br>
 *
 * @author Jouwerzi<br>
 * @version 1.0<br>
 * @taskId: AES可逆加密算法工具类<br>
 * @createDate 2019/11/08 10:42 <br>
 * @see com.tianruan.psychological.common.utils.encryption <br>
 *     encrypt是加密方法  decrpyt是解密方法
 * AES 是一种可逆加密算法，对用户的敏感信息加密处理 对原始数据进行AES加密后，在进行Base64编码转化；
 */
public class AESOperatorUtil{

    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/NoPadding";
    private static final String SECRET_KEY = "TrSoftTechnology"; //密钥必须得是16位的

    /**
     * @param String content //待加密内容
     * @return ${return_type}
     * @throws
     * @Description: 加密方法
     * @author Jouwerzi
     * @date 2019/11/8 11:12
     */
    public static String encrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = content.getBytes("UTF-8");
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(SECRET_KEY.getBytes(), KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(SECRET_KEY.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            byte[] msgBytes = Base64.encodeBase64(encrypted);
            return new String(msgBytes);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * @param String content //待解密内容
     * @return ${return_type}
     * @throws
     * @Description: 解密方法
     * @author Jouwerzi
     * @date 2019/11/8 11:11
     */
    public static String decrpyt(String content) {
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = Base64.decodeBase64(content);
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(SECRET_KEY.getBytes(), KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(SECRET_KEY.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            String result = new String(encrypted, "UTF-8");
            int lastChar = result.length();
            for (int i = result.length() - 1; i >= 0; i--) {
                if (result.charAt(i) != 0x00) {
                    break;
                }
                lastChar = i;
            }
            result = result.substring(0, lastChar);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

}
